package com.app.util;

import org.apache.tika.Tika;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Component
public class FileTypeCheckerUtil {

    public static boolean isAllowedFileType(MultipartFile file) {
        Tika tika = new Tika();
        String detectedType;
        try {
            detectedType = tika.detect(file.getInputStream());
        } catch (IOException e) {
            return false;
        }

        MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
        MimeType type;
        try {
            type = allTypes.forName(detectedType);
        } catch (MimeTypeException e) {
            return false;
        }
        String extension = type.getExtension();
        System.err.println("Detected file type: " + extension);

        String fileName = file.getOriginalFilename();
        int lastIndexOf = fileName.lastIndexOf(".");
        String suffix = fileName.substring(lastIndexOf);

        // 判断后缀是否为 jpg 以及一些可能的常见路径形式
        boolean fileNameType = suffix.equals(".jpg") || suffix.equals(".png") || suffix.equals(".jpeg") || suffix.equals(".gif") || suffix.equals(".bmp") || suffix.equals(".webp")
                || suffix.equals(".mp4") || suffix.equals(".mov");

        return extension.equals(".jpg") || extension.equals(".png") || extension.equals(".jpeg") || extension.equals(".gif") || extension.equals(".bmp") || extension.equals(".webp")
                || extension.equals(".mp4") || extension.equals(".mov") || fileNameType;
    }


    public static boolean isPictureFileType(MultipartFile file) {
        Tika tika = new Tika();
        String detectedType;
        try {
            detectedType = tika.detect(file.getInputStream());
        } catch (IOException e) {
            return false;
        }

        MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
        MimeType type;
        try {
            type = allTypes.forName(detectedType);
        } catch (MimeTypeException e) {
            return false;
        }
        String fileName = file.getOriginalFilename();
        int lastIndexOf = fileName.lastIndexOf(".");
        String suffix = fileName.substring(lastIndexOf);

        // 判断后缀是否为 jpg 以及一些可能的常见路径形式
        boolean fileNameType = suffix.equals(".jpg") || suffix.equals(".png") || suffix.equals(".jpeg") || suffix.equals(".gif") || suffix.equals(".bmp") || suffix.equals(".webp")
                || suffix.equals(".xls") || suffix.equals(".xlsx") || suffix.equals(".doc") || suffix.equals(".docx") || suffix.equals(".pdf");

        String extension = type.getExtension();
        System.err.println("Detected file type: " + extension);
        return extension.equals(".jpg") || extension.equals(".png") || extension.equals(".jpeg") || extension.equals(".gif") || extension.equals(".bmp") || extension.equals(".webp") || fileNameType;
    }


    public static boolean isFileType(MultipartFile file) {
        Tika tika = new Tika();
        String detectedType;
        try {
            detectedType = tika.detect(file.getInputStream());
        } catch (IOException e) {
            return false;
        }

        MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
        MimeType type;
        try {
            type = allTypes.forName(detectedType);
        } catch (MimeTypeException e) {
            return false;
        }

        String fileName = file.getOriginalFilename();
        int lastIndexOf = fileName.lastIndexOf(".");
        String suffix = fileName.substring(lastIndexOf);

        // 判断后缀是否为 jpg 以及一些可能的常见路径形式
        boolean fileNameType = suffix.equals(".xls") || suffix.equals(".xlsx") || suffix.equals(".doc") || suffix.equals(".docx") || suffix.equals(".pdf");

        String extension = type.getExtension();
        System.err.println("Detected file type: " + extension);
        return extension.equals(".xls") || extension.equals(".xlsx") || extension.equals(".doc") || extension.equals(".docx") || extension.equals(".pdf") || fileNameType;
    }

}
